<template>
  <div class="loginForm" @keyup="keyup">
    <span class="light-one"></span>
    <span class="light-two"></span>
    <div class="left">
<!--      <img src="../../../assets/logo.png" alt="" />-->
    </div>
    <div class="right">
      <img src="../../../assets/logo.png" class="logo" alt="" />
      <!--      <span class="title-s">镇江市京口区统一战线工作部</span>-->
      <span class="title-b"></span>
      <div class="label">
        <input type="text" v-model="username" placeholder="请输入账号" class="input" />
        <!--        <Icon type="md-person" color="#AAAAAA" size="20" class="picon"/>-->
        <i class="el-icon-user-solid picon"></i>
      </div>
      <div class="label">
        <input :type="isHidden ? 'password' : 'text'" v-model="password" placeholder="请输入密码" class="input" />
        <!--        <Icon type="md-lock" color="#AAAAAA" size="20" class="picon"/>-->
        <i class="el-icon-unlock picon"></i>
        <!--        <Icon type="md-eye" color="#AAAAAA" size="20" class="eye" @click="()=>{isHidden=!isHidden}" v-if="!isHidden"/>-->
        <!--        <Icon type="md-eye-off" color="#AAAAAA" size="20" class="eye" @click="()=>{isHidden=!isHidden}"-->
        <!--              v-if="isHidden"/>-->
      </div>
      <div class="label">
        <input type="text" v-model="validCode" placeholder="请输入验证码" class="input" />
        <i class="el-icon-mobile-phone picon"></i>
        <!--        <Icon type="logo-css3" color="#AAAAAA" size="20" class="picon"/>-->
        <div class="erweiCode">
          <img :src="imgBase64" @click="getDigitalCaptcha" alt="" />
        </div>
      </div>
      <div class="submit flex-center" v-loading="loading">
        <span @click="submit">登 录</span>
      </div>
    </div>
  </div>
</template>

<script>
// import store from "../../../store";
// import base64 from '@/util/base64.js'
// import util from '../../../util/util.js'

export default {
  name: "loginForm",
  data() {
    return {
      imgBase64: "",
      username: "",
      password: "",
      rememberme: 0,
      validCode: "",
      loading: false,
      isHidden: true,
    };
  },
  methods: {
    keyup(e) {
      // console.log(e);
      if (e.keyCode == 13) {
        this.login();
      }
    },
    getDigitalCaptcha() {
      // console.log("success");
      this.$api.getDigitalCaptcha().then((res) => {
        // console.log("success");
        window.biyiCaptchaKey = Object.keys(res)[0];
        this.imgBase64 = res[window.biyiCaptchaKey];
      });
    },
    login() {
      this.loading = true;
      window.biyiCaptcha = this.validCode;
      let params = {
        username: this.username,
        password: this.password,
        validCode: this.validCode,
        data: this.data,
        rememberme: this.rememberme,
      };
      this.$api
        .login(params)
        .then((res) => {
          this.loading = false;
          this.$store.dispatch("setIsLogin", true);
          sessionStorage.setItem("token", res.token);
          this.$api.getUser({}).then((res) => {
            this.$store.dispatch("setUserInfo", res.data);
            localStorage.setItem("userInfo", JSON.stringify(res.data));
            this.$router.push({name:'home'})
          });
        })
        .catch((err) => {
          this.loading = false;
          if (err.response) {
            switch (err.response.status) {
              case 401:
                if (err.response.data.attempts) {
                  this.$message.error(
                    `登录失败，您还有 ${err.response.data.attempts} 尝试机会！`
                  );
                } else {
                  const locktime = this.formatLockedTime(
                    err.response.data.lockoutTime
                  );
                  this.$message.error({
                    dangerouslyUseHTMLString: true,
                    message: `登录失败，您的账号已被锁定，请在<span style="font-weight: bold; color: #2d8cf0;">${locktime}</span>后重试！`,
                  });
                }
                break;
              default:
                this.$message.error(`登录失败，${err.response.data.message}！`);
                break;
            }
          }
          this.getDigitalCaptcha();
        });
    },
    submit() {
      this.login();
    },
    formatLockedTime(time) {
      let str = "";
      const d = parseInt(time / (3600 * 24));
      let h = parseInt((time % (3600 * 24)) / 3600);
      let m = parseInt((time % 3600) / 60);
      let s = (time % 3600) % 60;
      if (d > 0) {
        str = `${str}${d}天`;
      }
      if (h > 0) {
        str = `${str}${h}小时`;
      }
      if (m > 0) {
        str = `${str}${m}分`;
      }
      if (s > 0) {
        str = `${str}${s}秒`;
      }
      return str;
    },
  },
  mounted() {
    this.getDigitalCaptcha();
  },
};
</script>

<style scoped lang="less">
.loginForm {
  width: 1050px;
  height: 500px;
  border-radius: 8px;
  display: flex;
  position: relative;
  overflow: hidden;

  .title-s {
    font-size: 14px;
    color: #333333;
    font-weight: 500;
  }

  .title-b {
    font-size: 22px;
    color: #333333;
    font-weight: 500;
    display: inline-block;
    margin: 0 0 30px;
  }

  .logo {
    margin-top: 70px;
    //width: 50px;
    height: 24px;
  }

  .input {
    outline: none;
    border: 1px solid #EEEEEE;
    border-radius: 4px;
    width: calc(100% - 60px);
    // width: 100%;
    height: 60px;
    padding: 0 30px;
    background-color: transparent;

    &:focus {
      border: 1px solid #237ef7;
    }
  }

  .left {
    width: 650px;
    height: 100%;
    border-top-left-radius: 16px;
    border-bottom-left-radius: 16px;

    img {
      width: 650px;
      height: 100%;
    }
  }

  .right {
    width: 330px;
    height: 100%;
    padding: 0 40px;
    background-color: white;
    display: flex;
    flex-direction: column;
    align-items: center;

    .title {
      display: inline-block;
      font-size: 21px;
      font-weight: 500;
      margin-top: 40px;
    }

    .anotherLogin {
      img {
        border-radius: 100%;
        width: 50px;
        height: 50px;
        cursor: pointer;
      }
    }

    .label {
      width: 100%;
      padding: 15px 20px 0;
      position: relative;
      i{
        font-size: 20px;
      }

      .picon {
        position: absolute;
        top: 35px;
        left: 30px;
      }

      .eye {
        position: absolute;
        top: 38px;
        right: 20px;
        cursor: pointer;
      }

      .erweiCode {
        width: 120px;
        height: 59px;
        position: absolute;
        top: 16px;
        right: 20px;
        cursor: pointer;

        img {
          width: 100%;
          height: 100%;
        }
      }
    }

    .submit {
      margin-top: 30px;
      width: 260px;
      height: 50px;
      background: #187afb;
      box-shadow: 0px 5px 7px 0px rgba(24, 122, 251, 0.36);
      border-radius: 25px;
      color: white;
      font-size: 18px;
      font-weight: 500;
      letter-spacing: 10px;
      cursor: pointer;
      position: relative;

      &:hover {
        box-shadow: none;
      }

      span {
        display: inline-block;
        width: 100%;
        height: 100%;
        text-align: center;
        line-height: 50px;
      }
    }
  }
}

@keyframes loginLeft {
  0% {
    left: -100%;
  }

  50%,
  100% {
    left: 100%;
  }
}

@keyframes loginTop {
  0% {
    top: -100%;
  }

  50%,
  100% {
    top: 100%;
  }
}

@keyframes loginRight {
  0% {
    right: -100%;
  }

  50%,
  100% {
    right: 100%;
  }
}

@keyframes loginBottom {
  0% {
    bottom: -100%;
  }

  50%,
  100% {
    bottom: 100%;
  }
}
</style>
